<?php

namespace Controller\Ajax;

class User extends Base {

	public function saveUserInfo() {
		$this->mustLogin();
		$y = $this->postInt('y');
		$m = $this->postInt('m');
		$d = $this->postInt('d');
		$experience = $this->postInt('experience');
		$sex = $this->postInt('sex');
		$city = $this->postInt('city');
		$username = $this->postParam('username', true);
		$notion = $this->postParam('notion', true);
		$desc = $this->postParam('desc', true);
		$birthday = 0;
		if ($y && $m && $d) {
			$birthday = strtotime($y . '-' . $m . '-' . $d . ' 00:00:00');
		}
		$userId = $this->currentUserInfo['UserId'];
		try {
			\Business\User::instance()->saveUserInfo($userId, $username, $sex, $birthday, $experience, $city, $notion, $desc);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage());
		}
		$this->response(true);
	}

	public function saveCondition() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$city = $this->postInt('city');
		$age = $this->postInt('age');
		$sex = $this->postInt('sex');
		$weight = $this->postInt('weight');
		$length = $this->postInt('length');
		$height = $this->postInt('height');
		$color = $this->postParam('color', true);
		$breed = $this->postParam('breed', true);
		$breed = trim($breed);
		\Business\User::instance()->saveCondition($userId, $city, $age, $sex, $weight, $length, $height, $color, $breed);
		$this->response(true);
	}

	public function saveMaster() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$nickname = $this->postParam('nickname', true);
		$nickname = trim($nickname);
		$nickname = \Lib\Util\Comm::truncate($nickname, 32, '');
		$sex = $this->postInt('sex');
		$experience = $this->postInt('experience');
		$dogNumber = $this->postInt('dogNumber');
		$profession = $this->postParam('profession', true);
		$profession = trim($profession);
		$profession = \Lib\Util\Comm::truncate($profession, 30, '');
		$desc = $this->postParam('desc', true);
		$desc = trim($desc);
		$desc = \Lib\Util\Comm::truncate($desc, 500, '');
		$y = $this->postInt('y');
		$m = $this->postInt('m');
		$d = $this->postInt('d');
		$birthday = 0;
		if ($y && $m && $d) {
			$birthday = strtotime($y . '-' . $m . '-' . $d . ' 00:00:01');
		}
		if (!$nickname) {
			$this->responseError('请输入主人的昵称！');
		}
		if (!$sex) {
			$this->responseError('请选择主人性别！');
		}
		if (!$birthday) {
			$this->responseError('请选择主人生日！');
		}
		\Business\User::instance()->saveUserMaster($userId, $nickname, $sex, $experience, $dogNumber, $profession, $birthday, $desc);
		$this->response(true);
	}

	public function addPhoto() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$title = $this->postParam('title', true);
		$title = \Lib\Util\Comm::truncate($title, 128, '');
		$desc = $this->postParam('desc', true);
		$desc = \Lib\Util\Comm::truncate($desc, 500, '');
		$path = $this->postParam('path', true);
		$photoId = $this->postInt('photoId');
		if ($photoId) {
			\Business\User::instance()->updatePhoto($userId, $photoId, $title, $desc);
		} else {
			\Business\User::instance()->addPhoto($userId, $title, $desc, $path);
		}
		$this->response(true);
	}

	public function getBreed() {
		$breeds = \Business\Breed::instance()->getBreedName();
		$this->response(array_values($breeds));
	}

	public function delPhoto() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$photoId = $this->postInt('id');
		\Business\User::instance()->delPhoto($userId, $photoId);
		$this->response(true);
	}

	public function follow() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$followUserId = $this->postInt('userId');
		if (!$followUserId) {
			$this->responseError('参数错误');
		}
		try {
			\Business\User::instance()->follow($userId, $followUserId);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage(), $e->getCode());
		}
		$this->response(true);
	}

	public function followDel() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$followUserId = $this->postInt('userId');
		if (!$followUserId) {
			$this->responseError('参数错误');
		}
		try {
			\Business\User::instance()->followDel($userId, $followUserId);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage(), $e->getCode());
		}
		$this->response(true);
	}

    public function hello() {
        $this->mustLogin();
        $userId = $this->currentUserInfo['UserId'];
        $toUserId = $this->postInt('userId');
        $content = $this->postParam('content', true);
        if (!$toUserId) {
            $this->responseError('参数错误');
        }
        try {
            \Business\User::instance()->addHello($userId, $toUserId, $content);
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
        $this->response(true);
    }

    public function delFollow() {
        $this->mustLogin();
        $followUserId = $this->postInt('userId');
        $userId = $this->currentUserInfo['UserId'];
        \Business\User::instance()->delFollowUser($userId, $followUserId);
        $this->response(true);
    }

}